<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'center'">
        <ul id="tt" class="easyui-tree">
        </ul>
    </div>
    <div data-options="region:'south',border:false" style="text-align:right;padding:5px">
        <a class="easyui-linkbutton authorize" data-options="iconCls:'icon-lock'" href="javascript:;"
           style="width:80px">授权</a>
        <a class="easyui-linkbutton" data-options="iconCls:'fa fa-close '" href="javascript:;"
           onclick="javascript:$('#win').window('close')" style="width:80px">取消</a>
    </div>
</div>

<script type="text/javascript">

    $('#tt').tree({
        url: '/role/roleTree',
        checkbox: true,
        loadFilter: function (msg, parent) {
            if ("SUCCESS" == msg.state) {
                $.each(msg.data, function (i, v) {
                    v.text = v.name;
                    /*$.each(v.children, function (j, o) {
                     o.text = o.name;
                     });*/
                });
                return msg.data;
            }
        },
        onLoadSuccess: function (node, datas) {
            //获取用户ID
            var rows = $('#list').datagrid('getChecked');
            $.post("/userRole/listByUserId", {id: rows[0].id}, function (data) {
                var $aids = new Array();
                if (data.state == "SUCCESS") {
                    $.each(data.data, function (i, v) {
                        var node = $('#tt').tree('find', v.roleId);
                        if ($('#tt').tree('isLeaf', node.target)) {
                            $('#tt').tree('check', node.target);
                        }
                    });
                }
            });
        }
    });


    //分配已选权限
    $(".authorize").click(function () {
        //获取角色ID
        var rows = $('#list').datagrid('getChecked');
        //获取权限ID
        var $ids = new Array();
        var authorityUrl = "/userRole/assign";
        var nodes = $('#tt').tree('getChecked', ['checked', 'indeterminate']);
        $.each(nodes, function (i, v) {
            $ids.push(v.id);
        });
        $.post(authorityUrl, {
            rids: $ids,
            uid: rows[0].id
        }, function (data) {
            $.messager.show({
                title: '操作提示',
                msg: data.message,
                timeout: 2000,
                showType: 'slide'
            });
            if (data.state == "SUCCESS") {
                $('#win').window('close');
            }
        });
    })
</script>
